import { createAsyncThunk, createSlice } from "@reduxjs/toolkit"
import axios from "axios"

interface RecommendProductsState {
  loading: boolean,
  error: string | null,
  productList: any[]
}

const initialState: RecommendProductsState = {
  loading: true,
  error: null,
  productList:[]
}


export const getRecommendProducts = createAsyncThunk(
  'recommendProducts/getRecommendProducts',
  async () => {
    let url = `http://82.157.43.234:8080/api/productCollections`
    const {data} = await axios.get(url,{
      headers: {
        'x-icode': 'EDE45279EB142953'
      }
    })
    return data

  }
)

export const recommendProductsSlice = createSlice({
  name: 'recommendProducts',
  initialState,
  reducers: {
   
  },
  extraReducers: (builder) => {
    builder
      .addCase(getRecommendProducts.pending, (state) => {
        state.loading = true
      })
      .addCase(getRecommendProducts.fulfilled, (state, action) => {
        state.productList = action.payload
        state.loading = false
        state.error = null
      })
      .addCase(getRecommendProducts.rejected, (state, action) => {
        state.loading = false
        if (typeof action.payload === 'string') {
          state.error = action.payload
        } else {
          state.error = null
        }

      })
  }
})